package cn.iocoder.yudao.module.promotion.controller.admin.wechatservice;

import cn.iocoder.yudao.module.promotion.controller.admin.wechatservice.vo.*;
import cn.iocoder.yudao.module.promotion.convert.wechatservice.WechatServiceConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.wechatservice.WechatServiceDO;
import cn.iocoder.yudao.module.promotion.service.wechatservice.WechatServiceService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

@Tag(name = "管理后台 - 微信客服")
@RestController
@RequestMapping("/promotion/wechat-service")
@Validated
public class WechatServiceController {

    @Resource
    private WechatServiceService wechatServiceService;

    @PostMapping("/create")
    @Operation(summary = "创建微信客服")
    @PreAuthorize("@ss.hasPermission('zfkj:wechat-service:create')")
    public CommonResult<Long> createWechatService(@Valid @RequestBody WechatServiceCreateReqVO createReqVO) {
        return success(wechatServiceService.createWechatService(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信客服")
    @PreAuthorize("@ss.hasPermission('zfkj:wechat-service:update')")
    public CommonResult<Boolean> updateWechatService(@Valid @RequestBody WechatServiceUpdateReqVO updateReqVO) {
        wechatServiceService.updateWechatService(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信客服")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:wechat-service:delete')")
    public CommonResult<Boolean> deleteWechatService(@RequestParam("id") Long id) {
        wechatServiceService.deleteWechatService(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信客服")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zfkj:wechat-service:query')")
    public CommonResult<WechatServiceRespVO> getWechatService(@RequestParam("id") Long id) {
        WechatServiceDO wechatService = wechatServiceService.getWechatService(id);
        return success(WechatServiceConvert.INSTANCE.convert(wechatService));
    }

    @GetMapping("/list")
    @Operation(summary = "获得微信客服列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('zfkj:wechat-service:query')")
    public CommonResult<List<WechatServiceRespVO>> getWechatServiceList(@RequestParam("ids") Collection<Long> ids) {
        List<WechatServiceDO> list = wechatServiceService.getWechatServiceList(ids);
        return success(WechatServiceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信客服分页")
    @PreAuthorize("@ss.hasPermission('zfkj:wechat-service:query')")
    public CommonResult<PageResult<WechatServiceRespVO>> getWechatServicePage(@Valid WechatServicePageReqVO pageVO) {
        PageResult<WechatServiceDO> pageResult = wechatServiceService.getWechatServicePage(pageVO);
        return success(WechatServiceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信客服 Excel")
    @PreAuthorize("@ss.hasPermission('zfkj:wechat-service:export')")
    //@OperateLog(type = EXPORT)
    public void exportWechatServiceExcel(@Valid WechatServiceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WechatServiceDO> list = wechatServiceService.getWechatServiceList(exportReqVO);
        // 导出 Excel
        List<WechatServiceExcelVO> datas = WechatServiceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "微信客服.xls", "数据", WechatServiceExcelVO.class, datas);
    }

    @PutMapping("/close")
    @Operation(summary = "关闭推广链接")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> closeWechatService(@RequestParam("id") Long id) {
        wechatServiceService.closeWechatService(id);
        return success(true);
    }
}
